<template>
  <div class="app-container">
    <div class="top">
      <div class="left"><img src="@/assets/images/icon/leftIcon.png" alt="" srcset=""
          style="width: 15px;height: 14px;">模板库</div>
      <div class="right">
        <el-button type="primary" size="mini" @click="handleForceLogout()"
          v-hasPermi="['system:templateType:add']">新增</el-button>
      </div>
    </div>
    <el-form :model="search" ref="queryForm" size="small" :inline="true" label-width="180px">
      <el-form-item label="请输入搜索模板类型名称：" prop="ipaddr">
        <el-input v-model="search.name" placeholder="模板类型名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>
    <!-- <el-table v-loading="loading" :data="list.slice((pageNum - 1) * pageSize, pageNum * pageSize)" style="width: 100%;"
      default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"> -->
    <el-table :data="list.slice((pageNum - 1) * pageSize, pageNum * pageSize)" style="width: 100%;margin-bottom: 20px;"
      border default-expand-all>
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="模板类型" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="update(scope.row)"
            v-hasPermi="['system:templateType:query']">编辑</el-button>
          <span class="delete"> <el-button size="mini" type="text" @click="deletRow(scope.row)"
              v-hasPermi="['system:templateType:remove']">删除</el-button></span>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" row-key="id" border default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column prop="date" label="日期" sortable width="180">
      </el-table-column>
      <el-table-column prop="name" label="姓名" sortable width="180">
      </el-table-column>
      <el-table-column prop="address" label="地址">
      </el-table-column>
    </el-table> -->
    <pagination v-show="total > 0" :total="total" :page.sync="search.pageNum" :limit.sync="search.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%" @close="closeDialog">
      <el-form :model="form">
        <div class="item">
          <el-form-item :label="subTitle" :label-width="formLabelWidth" label-position="left">
            <!-- {{ i.name }} -->
            <el-input v-model="name" autocomplete="off"></el-input>
          </el-form-item>
          <!-- <div class="close" @click="closeItem()"><i class="el-icon-close" size="30"></i></div> -->
        </div>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <div class="left"></div>
        <div class="right"> <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addInfo" v-hasPermi="['system:templateType:edit']">确 定</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { insertType, list, deleteTemplate, updateTemplate, search, listType } from "@/api/template/template";
import { Message } from 'element-ui'
export default {
  name: "Online",
  data() {
    return {
      search: {
        name: '',
        pageNum: 1,
        pageSize: 10,
      },
      searchName: '',
      subTitle: '类型名称',
      title: '添加类型',
      name: '',
      dialogFormVisible: false,
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      infolist: [{ name: '选项内容' }],
      formLabelWidth: '100px',
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      pageNum: 1,
      pageSize: 10,
      // 查询参数
      queryParams: {
        "parentName": ''
      },
      templateId: '',
      deletId: '',
      updateData: {
        name: '',
        templateId: ''
      },
      isUpdate: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    closeDialog() {
      this.isUpdate = false;
      this.templateId = ''
      this.name = ''
      this.subTitle = '类型名称'
      this.title = '添加类型'
      console.log('close')
    },
    async update(row) {
      console.log(row, 'row')
      this.subTitle = '类型名称'
      this.title = '修改类型'
      this.name = row.name
      this.updateData.templateTypeId = row.templateTypeId
      this.isUpdate = true;
      this.dialogFormVisible = true
    },
    closeItem(e) {
      this.infolist.splice(e, 1)
    },
    async updateFn() {
      this.updateData.name = this.name
      console.log(this.updateData, 'updateData')
      let res = await updateTemplate(this.updateData)
      this.dialogFormVisible = false
      Message({ message: '修改成功', type: 'success' })
      this.name = ''
      this.isUpdate = false;
      this.getList()

    },
    async addInfo() {
      if (this.isUpdate) {
        this.updateFn()
        return false;
      }
      let data = {
        name: this.name
      }
      let res = await insertType(data)
      this.dialogFormVisible = false
      Message({ message: '添加成功', type: 'success' })
      this.name = ''
      this.getList()
    },
    async deleteById(id) {
      let res = await deleteTemplate(id)
      // Message({ message: '删除成功', type: 'success' })
      this.name = ''
      this.getList()
    },
    deletRow(row) {
      this.$modal.confirm('是否确认删除名称为"' + row.name + '"的操作？').then(() => {
        // return this.deleteById(row.templateId);
        return deleteTemplate([row.templateTypeId]);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      // let data = {
      //   name: this.searchName
      // }
      // console.log(data)
      listType(this.search).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    async handleQuery() {
      this.getList()
      // this.pageNum = 1;
      // this.loading = true;
      // search(this.queryParams).then(response => {
      //   this.list = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleForceLogout(row) {
      this.name = ''
      this.dialogFormVisible = true
    }
  }
};
</script>

<style scoped lang="scss">
::v-deep .delete .el-button--text {
  color: #FF4431 !important;
  margin-left: 10px;
}

.top {
  display: flex;
  justify-content: space-between;
  color: #333;
  margin-bottom: 15px;

  .left {
    img {
      margin-right: 10px;
    }
  }

  .right {}
}

.dialog-footer {
  display: flex;
  justify-content: space-between;
}

.item {
  display: flex;
  align-items: center;
  position: relative;
  top: -10px;
  left: 10px;

  .close {
    position: relative;
    top: -10px;
    left: 10px;
    font-size: 20px;
    cursor: pointer;
  }
}
</style>
